package com.yonyou.cyxdms.basedata.service.service.color;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.jsonserializer.JSONUtil;
import com.yonyou.cyxdms.basedata.client.domains.vo.ColorCacheVO;
import com.yonyou.cyxdms.basedata.service.dto.CarparamcolorDTO;
import com.yonyou.cyxdms.basedata.service.entity.color.ColorPO;
import com.yonyou.cyxdms.basedata.service.repository.color.ColorMapper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsx
 * @since 2018-09-07
 */
@Service
public class CarparamcolorServiceImpl extends ServiceImpl<ColorMapper, ColorPO> implements CarparamcolorService {
    //日志对象
    public static final Logger logger = LoggerFactory.getLogger(CarparamcolorServiceImpl.class);

    @Autowired
    ColorMapper carparamcolorMapper;

    /**
     * 分页查询对应数据
     *
     * @param page           分页对象
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               <                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               java.lang.String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               java.lang
                       *       .                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Object>>
     * @author zhangsx
     * @since 2018/7/22 0022
     */
    @Override
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(carparamcolorMapper.selectPageBySql(page, queryCondition));
        return page;
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map                                                                                                                               <                                                                                                                               java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>>
     * @author zhangsx
     * @since 2018/7/22 0022
     */
    @Override
    public List<Map<String, Object>> selectListBySql(Map<String, Object> queryCondition) {
        return carparamcolorMapper.selectListBySql(queryCondition);
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param id 主键ID
     * @return java.util.List<java.util.Map                                                                                                                                                                                                                                                               <                                                                                                                                                                                                                                                               java.lang.String                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                               java.lang.Object>>
     * @author zhangsx
     * @since 2018/7/22 0022
     */
    @Override
    public Map<String, Object> getById(Integer id) {
        ColorPO carParamColorPo = carparamcolorMapper.selectById(id);
        if (carParamColorPo != null) {
            return carParamColorPo.toMaps();
        } else {
            return null;
        }
    }

    /**
     * 根据DTO 进行数据新增
     *
     * @param carparamcolorDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String, Object> insert(CarparamcolorDTO carparamcolorDTO) {
        //对对象进行赋值操作
        ColorPO carParamColorPo = carparamcolorDTO.transDtoToPo(ColorPO.class);
        //执行插入
        carparamcolorMapper.insert(carParamColorPo);
        //返回插入的值
        return carparamcolorMapper.selectById(carParamColorPo.getColorId()).toMaps();
    }

    /**
     * 根据DTO 及ID 进行数据更新
     *
     * @param id               主键ID
     * @param carparamcolorDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String, Object> update(Integer id, CarparamcolorDTO carparamcolorDTO) {
        ColorPO carParamColorPo = carparamcolorMapper.selectById(id);
        //对对象进行赋值操作
        carparamcolorDTO.transDtoToPo(carParamColorPo);
        //执行更新
        carparamcolorMapper.updateById(carParamColorPo);
        return carParamColorPo.toMaps();
    }

    /**
     * 根据车型Id  颜色Id 检索指导价格
     *
     * @param carId
     * @param colorId
     * @return (non - Javadoc)
     * @author Zhangsx
     * @date 2018年9月7日
     */
    @Override
    public Double queryPrice(Long carId, Integer colorId) {
        LambdaQueryWrapper<ColorPO> queryWrapper = new QueryWrapper<ColorPO>().lambda();
        queryWrapper.eq(ColorPO::getColorId, colorId);

        //获取loginInfoDto
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String appid = loginInfoDto.getAppId();
        logger.debug("appid:{}", appid);
        queryWrapper.eq(ColorPO::getAppId, appid);
        List<ColorPO> list = carparamcolorMapper.selectList(queryWrapper);
        if (list.size() != 1) {
            throw new BeanInitializationException("车型报价数据异常，请重试");
        }
        return 0D;
    }

    /**
     * 查询颜色信息
     *
     * @param map
     * @return java.util.Map<java.lang.String       ,               java.lang.Object>
     * @author renwd
     * @since 2018/11/26
     */
    @Override
    public Map<String, Object> queryColorByCondition(Map<String, Object> map) {
        List<ColorCacheVO> colorCacheDto = carparamcolorMapper.queryColorByCondition(map);
        return JSONUtil.jsonToMap(JSONUtil.objectToJson(colorCacheDto));
    }

}
